// src/utils/AnalysisTools.ts
import { SuperMap3D } from '@/utils/supermap3d';

interface ProfileData {
  startPoint: [number, number, number];
  endPoint: [number, number, number];
  extendHeight: number;
  getBuffer: (callback: (buffer: Uint8ClampedArray) => void) => void;
  build: () => void;
  _textureHeight: number;
  _textureWidth: number;
}

export class ProfileTools {
  private viewer: any;
  private profile: ProfileData;
  private handlerLine: any; //SuperMap3D.DrawHandler
  private tooltip?: any;

  constructor(viewer: any, profile: ProfileData, tooltip: any) {
    this.viewer = viewer;
    this.profile = profile;
    this.tooltip = tooltip;

    this.handlerLine = new SuperMap3D.DrawHandler(this.viewer, SuperMap3D.DrawMode.Line);

    this.initEvents();
  }

  private initEvents(): void {
    this.handlerLine.activeEvt.addEventListener((isActive: boolean) => {
      if (isActive) {
        this.viewer.enableCursorStyle = false;
        this.viewer._element.style.cursor = '';
        document.body.classList.remove('drawCur');
        document.body.classList.add('drawCur');
      } else {
        this.viewer.enableCursorStyle = true;
        document.body.classList.remove('drawCur');
      }
    });

    this.handlerLine.movingEvt.addEventListener((windowPosition: any) => {
      // if (this.handlerLine.isDrawing) {
      //   this.tooltip.showAt(windowPosition, '<p>点击绘制第二个点，右键单击结束绘制</p>');
      // } else {
      //   this.tooltip.showAt(windowPosition, '<p>点击绘制第一个点</p>');
      // }
      this.tooltip?.showAt(windowPosition, this.handlerLine.isDrawing ? '点击绘制第二个点，右键结束' : '点击绘制第一个点');
    });

    this.handlerLine.drawEvt.addEventListener((result: any) => {
      this.tooltip.setVisible(false);

      const line = result.object;
      if (line.positions.length < 2) {
        return;
      }

      const [startPoint, endPoint] = line.positions;
      this.handlerLine.clear();

      const sCarto = SuperMap3D.Cartographic.fromCartesian(startPoint);
      const eCarto = SuperMap3D.Cartographic.fromCartesian(endPoint);

      const sLon = SuperMap3D.Math.toDegrees(sCarto.longitude);
      const sLat = SuperMap3D.Math.toDegrees(sCarto.latitude);
      const sHeight = sCarto.height;

      const eLon = SuperMap3D.Math.toDegrees(eCarto.longitude);
      const eLat = SuperMap3D.Math.toDegrees(eCarto.latitude);
      const eHeight = eCarto.height;

      this.profile.startPoint = [sLon, sLat, sHeight];
      this.profile.endPoint = [eLon, eLat, eHeight];
      this.profile.extendHeight = 40;

      this.profile.getBuffer((buffer: Uint8ClampedArray) => {
        const canvas = document.getElementById('pro') as HTMLCanvasElement;
        canvas.height = this.profile._textureHeight;
        canvas.width = this.profile._textureWidth;

        const ctx = canvas.getContext('2d');
        if (ctx) {
          const imgData = ctx.createImageData(canvas.width, canvas.height);
          imgData.data.set(buffer);
          ctx.putImageData(imgData, 0, 0);
        }

        canvas.style.width = '600px';
        canvas.style.height = '450px';
      });

      this.profile.build();
    });
  }

  public activate(): void {
    this.clear();
    this.handlerLine.activate();
  }

  public clear(): void {
    this.handlerLine.clear();
    const canvas = document.getElementById('pro') as HTMLCanvasElement;
    if (canvas) {
      canvas.style.width = '0px';
      canvas.style.height = '0px';
    }
  }
}
